import ajax from "./module/ajax.js";
import { renderImgList, NewSwiper } from "./module/tools.js";

let swiper = document.querySelector("#swiper");
let wrapper = document.querySelector("#swiper .swiper-wrapper");

let adsImg = document.querySelectorAll(".adsImg");

let childImg = document.querySelectorAll(".childImg");

let codeImg = document.querySelectorAll(".codeImg");

let token = null;
const API = 'http://website.bluej.cn';

window.onload = async function () {
    //  登陆请求获取token
    try {
        let res = await ajax({
            url: API + '/api/admin/login',
            method: 'POST',
            data: {
                phone: '13302408576',
                password: '123456'
            }
        });
        token = res.data.token;

    } catch (error) {
        console.log(error);
    }

    // banner动态获取轮播图
    try {
        let getCarousel = await ajax({
            url: API + '/api/carouselGroup/get',
            data: {
                carouselGroupId: 339
            },
            headers: {
                'x-token': token
            }
        })
        let carouselList = getCarousel.data.Carousels;
        //传入需要添加轮播图组的节点和轮播图的url数组
        renderImgList(wrapper, carouselList);
        //创建swiper
        var mySwiper = NewSwiper(swiper, { speed: 1500 });
    } catch (error) {
        console.log(error);
    }

    // 动态获取广告图片
    try {
        let getAdsImg = await ajax({
            url: API + '/api/carouselGroup/get',
            headers: {
                "x-token": token
            },
            data: {
                carouselGroupId: 346
            }
        })
        let adsImgList = getAdsImg.data.Carousels;
        for (let i = 0; i < adsImgList.length; i++) {
            let Img = new Image();
            Img.src = adsImgList[i].url;
            adsImg[i].appendChild(Img)
        }
    } catch (error) {
        console.log(error);
    }

    // 动态获取洁牙菜单栏图片
    try {
        let getChildImg = await ajax({
            url: API + '/api/carouselGroup/get',
            headers: {
                "x-token": token
            },
            data: {
                carouselGroupId: 347
            }
        })
        let childImgList = getChildImg.data.Carousels;
        for (let i = 0; i < childImgList.length; i++) {
            let Img = new Image();
            Img.src = childImgList[i].url;
            childImg[i].appendChild(Img)
        }
    } catch (error) {
        console.log(error);
    }

    // 动态获取二维码图片
    try {
        let getCodeImg = await ajax({
            url: API + '/api/carouselGroup/get',
            headers: {
                "x-token": token
            },
            data: {
                carouselGroupId: 343
            }
        })
        let codeImgList = getCodeImg.data.Carousels;
        for (let i = 0; i < codeImgList.length; i++) {
            let Img = new Image();
            Img.src = codeImgList[i].url;
            codeImg[i].appendChild(Img)
        }
    } catch (error) {
        console.log(error);
    }
}